package com.Home.digital_life;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class ContactPersonActivity extends Activity {

	Handler mHandler = null;
	boolean isActive;
	VCardIO vcarIO = null;
	int mLastProgress;
	CheckBox mReplaceOnImport = null;
	Button importButton = null;
	Button exportButton = null;
	ProgressDialog progressDlg = null;
	private String importing = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contect);
		mHandler = new Handler();
		vcarIO = new VCardIO(this);
		// ��ʾ�����
		progressDlg = new ProgressDialog(this);
		progressDlg.setCancelable(false);
		progressDlg.setProgress(10000);

		importButton = (Button) findViewById(R.id.ImportButton);
		exportButton = (Button) findViewById(R.id.ExportButton);
		mReplaceOnImport = ((CheckBox) findViewById(R.id.ReplaceOnImport));

		OnClickListener listenImport = new OnClickListener() {
			public void onClick(View v) {
				if (vcarIO != null) {
					String fileName = ((EditText) findViewById(R.id.ImportFile))
							.getText().toString();
					// ���½��
					progressDlg.show();
					importing = "���ڵ���,���Ժ�...";
					updateProgress(0);
					vcarIO.doImport(fileName, mReplaceOnImport.isChecked(),
							ContactPersonActivity.this);
				}
			}
		};

		OnClickListener listenExport = new OnClickListener() {
			public void onClick(View v) {
				if (vcarIO != null) {
					String fileName = ((EditText) findViewById(R.id.ExportFile))
							.getText().toString();
					// ���½��
					progressDlg.show();
					importing = "���ڵ���,���Ժ�...";
					updateProgress(0);
					vcarIO.doExport(fileName, ContactPersonActivity.this);
				}
			}
		};
		importButton.setOnClickListener(listenImport);
		exportButton.setOnClickListener(listenExport);
	}

	/**
	 * ���½����
	 * 
	 * @param progress
	 *            ���
	 */
	public void updateProgress(final int progress) {
		mHandler.post(new Runnable() {
			public void run() {
				progressDlg.setProgress(progress * 100);
				progressDlg.setMessage(importing + progress + "%");
				if (progress == 100) {
					progressDlg.cancel();
				}

			}
		});
	}
}
